#include "stdafx.h"
#include "LuaBattle.h"

LuaBattle::LuaBattle()
{
}
LuaBattle::~LuaBattle()
{

}
void LuaBattle::Register()
{
	singleton<ScriptManager>::instance().RegisterBattle("AddMonsterAt",LuaBattle::luaAddMonsterAt);
	singleton<ScriptManager>::instance().RegisterBattle("SetMonsterAt",LuaBattle::luaSetMonsterAt);
	singleton<ScriptManager>::instance().RegisterBattle("RemoveMonster",LuaBattle::luaRemoveMonster);
	singleton<ScriptManager>::instance().RegisterBattle("LOGGER_ERROR",LuaBattle::luaLOGGER_ERROR);
	singleton<ScriptManager>::instance().RegisterBattle("LOGGER_INFO",LuaBattle::luaLOGGER_INFO);
	singleton<ScriptManager>::instance().RegisterBattle("GetRandomTargetIndex",LuaBattle::luaGetRandomTargetIndex);
	singleton<ScriptManager>::instance().RegisterBattle("AddSkillTarget",LuaBattle::luaAddSkillTarget);
	singleton<ScriptManager>::instance().RegisterBattle("AddSkillTargetState",LuaBattle::luaAddSkillTargetState);
	singleton<ScriptManager>::instance().RegisterBattle("AddSkillTargetStateEx",LuaBattle::luaAddSkillTargetStateEx);
	singleton<ScriptManager>::instance().RegisterBattle("AddEXPAllPlayer",LuaBattle::luaAddEXPAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("AddEXPAllPet",LuaBattle::luaAddEXPAllPet);
	singleton<ScriptManager>::instance().RegisterBattle("AddMoneyAllPlayer",LuaBattle::luaAddMoneyAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("AddWoodAllPlayer",LuaBattle::luaAddWoodAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("AddFoodAllPlayer",LuaBattle::luaAddFoodAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("AddItemAllPlayer",LuaBattle::luaAddItemAllPlayer);

	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerType",LuaBattle::luaGetBattlerType);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerJDHurt",LuaBattle::luaGetBattlerJDHurt);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerFDHurt",LuaBattle::luaGetBattlerFDHurt);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerGHHurt",LuaBattle::luaGetBattlerGHHurt);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerHP",LuaBattle::luaGetBattlerHP);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerHPMax",LuaBattle::luaGetBattlerHPMax);
	singleton<ScriptManager>::instance().RegisterBattle("SetBattlerHP",LuaBattle::luaSetBattlerHP);
	singleton<ScriptManager>::instance().RegisterBattle("SetBattlerHPChange",LuaBattle::luaSetBattlerHPChange);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerAP",LuaBattle::luaGetBattlerAP);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerDP",LuaBattle::luaGetBattlerDP);

	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerPropBJ",LuaBattle::luaGetBattlerPropBJ);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerPropSB",LuaBattle::luaGetBattlerPropSB);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerPropFJ",LuaBattle::luaGetBattlerPropFJ);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerPropLJ",LuaBattle::luaGetBattlerPropLJ);

	//singleton<ScriptManager>::instance().RegisterBattle("GetBattlerState",LuaBattle::luaGetBattlerState);
	singleton<ScriptManager>::instance().RegisterBattle("SendChatMessageBattler",LuaBattle::luaSendChatMessageBattler);
	singleton<ScriptManager>::instance().RegisterBattle("GetTime",LuaBattle::luaGetTime);
	//singleton<ScriptManager>::instance().RegisterBattle("SetBattleEndInfoAllPlayer",LuaBattle::luaSetBattleEndInfoAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("SendInfoAllPlayer",LuaBattle::luaSendInfoAllPlayer);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerEnhanceHL",LuaBattle::luaGetBattlerEnhanceHL);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerEnhanceBD",LuaBattle::luaGetBattlerEnhanceBD);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerEnhanceHS",LuaBattle::luaGetBattlerEnhanceHS);

	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerResistHL",LuaBattle::luaGetBattlerResistHL);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerResistBD",LuaBattle::luaGetBattlerResistBD);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerResistHS",LuaBattle::luaGetBattlerResistHS);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerResistJZ",LuaBattle::luaGetBattlerResistJZ);
	singleton<ScriptManager>::instance().RegisterBattle("GetBattlerResistSD",LuaBattle::luaGetBattlerResistSD);
	singleton<ScriptManager>::instance().RegisterBattle("ClearBattlerNegativeState",LuaBattle::luaClearBattlerNegativeState);

	//singleton<ScriptManager>::instance().RegisterBattle("GetTargetState",LuaBattle::luaGetTargetState);
	//singleton<ScriptManager>::instance().RegisterBattle("GetTargetStateRoundCount",LuaBattle::luaGetTargetStateRoundCount);
	singleton<ScriptManager>::instance().RegisterBattle("SetLearning",LuaBattle::luaSetLearning);
	singleton<ScriptManager>::instance().RegisterBattle("AddPlayerStamina",LuaBattle::luaAddPlayerStamina);

	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateNormal",LuaBattle::luaIsBattlerStateNormal);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateDead",LuaBattle::luaIsBattlerStateDead);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateTP",LuaBattle::luaIsBattlerStateTP);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateHL",LuaBattle::luaIsBattlerStateHL);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateBD",LuaBattle::luaIsBattlerStateBD);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateCM",LuaBattle::luaIsBattlerStateCM);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateSL",LuaBattle::luaIsBattlerStateSL);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateQS",LuaBattle::luaIsBattlerStateQS);
	singleton<ScriptManager>::instance().RegisterBattle("IsBattlerStateRM",LuaBattle::luaIsBattlerStateRM);
}

Battle* LuaBattle::GetBattle(lua_State * L)
{
	lua_getglobal(L, "Battle");
	if (lua_isnil(L,lua_gettop(L)))
		return NULL;
	return (Battle*)(lua_topointer(L, lua_gettop(L)));
}

Battle::Battler* LuaBattle::GetBattler(lua_State * L)
{
	lua_getglobal(L, "Battler");
	if (lua_isnil(L,lua_gettop(L)))
		return NULL;
	return (Battle::Battler*)(lua_topointer(L, lua_gettop(L)));
}
Int32 LuaBattle::luaGetTime(lua_State *L)
{
	lua_pushnumber(L, Int32(time(NULL)));
	return 1;
}
Int32 LuaBattle::luaAddMonsterAt(lua_State *L)
{
	//void Battle::AddMonsterAt(UInt32 nTeam,UInt32 index,MonsterId nCSVId,std::string strName,Int32 nImageId,Int32 nHPMax,Int32 nAP,Int32 nSP,
	//	Int32 nResistHY,Int32 nResistZS,Int32 nResistFD,Int32 nResistGH,Int32 nResistJD)
	Int32 nIndex = -15;
	Int32 nTeam = lua_tointeger(L, nIndex++);
	Int32 index = lua_tointeger(L, nIndex++);
	Int32 nCSVId = lua_tointeger(L, nIndex++);
	const char *szName = lua_tostring(L, nIndex++);
	if(szName == NULL)
	{
		LOGGER_ERROR("luaAddMonsterAt,szName == NULL");
		return 0;
	}
	Int32 nLevel = lua_tointeger(L, nIndex++);
	Int32 nImageId = lua_tointeger(L, nIndex++);
	Int32 nHP = lua_tointeger(L, nIndex++);
	Int32 nAP = lua_tointeger(L, nIndex++);
	Int32 nDP = lua_tointeger(L, nIndex++);
	Int32 nSP = lua_tointeger(L, nIndex++);
	Int32 nResistHL = lua_tointeger(L, nIndex++);
	Int32 nResistBD = lua_tointeger(L, nIndex++);
	Int32 nResistHS = lua_tointeger(L, nIndex++);
	Int32 nResistCF = lua_tointeger(L, nIndex++);
	Int32 nResistQS = lua_tointeger(L, nIndex++);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddMonsterAt,pBattle == NULL,nTeam:%d,index:%d,nCSVId:%d",nTeam,index,nCSVId);
		return 0;
	}

	//LOGGER_ERROR("luaAddMonsterAt,nTeam:%d,index:%d,nCSVId:%d,szName:%s,nCSVId:%d,nImageId:%d,nHPMax:%d,nAP:%d,nSP:%d",nTeam,index,nCSVId,szName,nImageId,nHP,nAP,nSP);
	pBattle->AddMonsterAt(nTeam,index,nCSVId,szName,nLevel,nImageId,nHP,nAP,nDP,nSP,nResistHL,nResistBD,nResistHS,nResistCF,nResistQS);
	lua_pushnumber(L, 0);
	return 1;
}
Int32 LuaBattle::luaSetMonsterAt(lua_State *L)
{
	Int32 nIndex = -15;
	Int32 nTeam = lua_tointeger(L, nIndex++);
	Int32 index = lua_tointeger(L, nIndex++);
	Int32 nCSVId = lua_tointeger(L, nIndex++);
	const char *szName = lua_tostring(L, nIndex++);
	if(szName == NULL)
	{
		LOGGER_ERROR("luaSetMonsterAt,szName == NULL");
		return 0;
	}
	Int32 nLevel = lua_tointeger(L, nIndex++);
	Int32 nImageId = lua_tointeger(L, nIndex++);
	Int32 nHP = lua_tointeger(L, nIndex++);
	Int32 nAP = lua_tointeger(L, nIndex++);
	Int32 nDP = lua_tointeger(L, nIndex++);
	Int32 nSP = lua_tointeger(L, nIndex++);
	Int32 nResistHL = lua_tointeger(L, nIndex++);
	Int32 nResistBD = lua_tointeger(L, nIndex++);
	Int32 nResistHS = lua_tointeger(L, nIndex++);
	Int32 nResistCF = lua_tointeger(L, nIndex++);
	Int32 nResistQS = lua_tointeger(L, nIndex++);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}

	pBattle->SetMonsterAt(nTeam,index,nCSVId,szName,nLevel,nImageId,nHP,nAP,nDP,nSP,nResistHL,nResistBD,nResistHS,nResistCF,nResistQS);
	lua_pushnumber(L, 0);
	return 1;
}
Int32 LuaBattle::luaRemoveMonster(lua_State *L)
{
	Int32 nIndex = -2;
	Int32 nTeam = lua_tointeger(L, nIndex++);
	Int32 index = lua_tointeger(L, nIndex++);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	
	pBattle->RemoveMonster(nTeam,index);
	return 0;
}
Int32 LuaBattle::luaLOGGER_ERROR(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	if (info.empty()) return 0;

	LOGGER_ERROR("%s",info.c_str());
	return 0;
}
Int32 LuaBattle::luaLOGGER_INFO(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	if (info.empty()) return 0;
	LOGGER_INFO("%s",info.c_str());
	return 0;
}
Int32 LuaBattle::luaGetRandomTargetIndex(lua_State *L)
{
	UInt32 nTeamSelf = lua_tointeger(L, -6);
	UInt32 nIndexSelf = lua_tointeger(L, -5);
	UInt32 nTeam = lua_tointeger(L, -4);
	Int32 nNeedLife = lua_tointeger(L, -3);
	Int32 nNotStateBD = lua_tointeger(L, -2);
	Int32 nIncludeSelf = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaGetRandomTargetIndex,pBattle == NULL,nTeam:%d",nTeam);
		return 0;
	}
	//LOGGER_INFO("luaGetRandomTargetIndex,nTeam:%d",nTeam);
	Int32 nIndex = pBattle->GetRandomTargetIndex(nTeamSelf,nIndexSelf,nTeam,nNeedLife > 0,nNotStateBD > 0,nIncludeSelf > 0);
	lua_pushnumber(L, nIndex);
	return 1;
}

Int32 LuaBattle::luaAddSkillTarget(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -3);
	Int32 nIndex = lua_tointeger(L, -2);
	Int32 nHPChange = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;

	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)return 0;

	LOGGER_INFO("luaAddSkillTarget,nTeam:%d,nIndex:%d,nHPChange:%d",nTeam,nIndex,nHPChange);
	pBattle->AddSkillTarget(pBattler,nTeam,nIndex,nHPChange);
	lua_pushnumber(L, 0);
	return 1;
}

Int32 LuaBattle::luaAddSkillTargetState(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -6);
	Int32 nIndex = lua_tointeger(L, -5);
	Int32 nHPChange = lua_tointeger(L, -4);
	Int32 nState = lua_tointeger(L, -3);
	Int32 nStateRoundCount = lua_tointeger(L, -2);
	Int32 nStateData = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;

	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)return 0;

	LOGGER_INFO("luaAddSkillTargetState,nTeam:%d,nIndex:%d,nHPChange:%d,nState:%d,nStateRoundCount:%d,nStateData:%d",nTeam,nIndex,nHPChange,nState,nStateRoundCount,nStateData);
	pBattle->AddSkillTarget(pBattler,nTeam,nIndex,nHPChange,nState,nStateRoundCount,nStateData);
	lua_pushnumber(L, 0);
	return 1;
}
Int32 LuaBattle::luaAddSkillTargetStateEx(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -13);
	Int32 nIndex = lua_tointeger(L, -12);
	Int32 nHPChange = lua_tointeger(L, -11);
	Int32 nState = lua_tointeger(L, -10);
	Int32 nStateRoundCount = lua_tointeger(L, -9);
	Int32 nStateData = lua_tointeger(L, -8);

	Int32 bBJ = lua_tointeger(L, -7);
	Int32 bSB = lua_tointeger(L, -6);
	Int32 bFJ = lua_tointeger(L, -5);
	Int32 nFJHPChange = lua_tointeger(L, -4);
	Int32 bLJ = lua_tointeger(L, -3);
	Int32 nLJHPChange1 = lua_tointeger(L, -2);
	Int32 nLJHPChange2 = lua_tointeger(L, -1);

	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;

	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)return 0;

	LOGGER_INFO("luaAddSkillTargetState,nTeam:%d,nIndex:%d,nHPChange:%d,nState:%d,nStateRoundCount:%d,nStateData:%d",nTeam,nIndex,nHPChange,nState,nStateRoundCount,nStateData);
	pBattle->AddSkillTarget(pBattler,nTeam,nIndex,nHPChange,nState,nStateRoundCount,nStateData,bBJ>0,bSB>0,bFJ>0,nFJHPChange,bLJ>0,nLJHPChange1,nLJHPChange2);
	lua_pushnumber(L, 0);
	return 1;
}
Int32 LuaBattle::luaAddEXPAllPlayer(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddEXPAllPlayer,pBattle == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_INFO("luaAddEXPAllPlayer,nValue:%d",nValue);
	pBattle->AddEXPAllPlayer(nValue);
	return 0;
}
Int32 LuaBattle::luaAddMoneyAllPlayer(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddMoneyAllPlayer,pBattle == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_INFO("luaAddMoneyAllPlayer,nValue:%d",nValue);
	pBattle->AddMoneyAllPlayer(nValue);
	return 0;
}
Int32 LuaBattle::luaAddItemAllPlayer(lua_State *L)
{
	Int32 nItemCSVId = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddItemAllPlayer,pBattle == NULL,nItemCSVId:%d",nItemCSVId);
		return 0;
	}
	pBattle->AddItemAllPlayer(nItemCSVId);
	return 0;
}
Int32 LuaBattle::luaAddWoodAllPlayer(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddWoodAllPlayer,pBattle == NULL,nValue:%d",nValue);
		return 0;
	}
	pBattle->AddWoodAllPlayer(nValue);
	return 0;
}
Int32 LuaBattle::luaAddFoodAllPlayer(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddFoodAllPlayer,pBattle == NULL,nValue:%d",nValue);
		return 0;
	}
	pBattle->AddFoodAllPlayer(nValue);
	return 0;
}
Int32 LuaBattle::luaAddEXPAllPet(lua_State *L)
{
	Int32 nValue = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		LOGGER_ERROR("luaAddEXPAllPet,pBattle == NULL,nValue:%d",nValue);
		return 0;
	}
	LOGGER_INFO("luaAddEXPAllPet,nValue:%d",nValue);
	pBattle->AddEXPAllPet(nValue);
	return 0;
}
Int32 LuaBattle::luaGetBattlerType(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nBattlerType);
	return 1;
}
Int32 LuaBattle::luaGetBattlerJDHurt(lua_State *L)
{
	//Int32 nValue = lua_tointeger(L, -1);
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nAP);
	return 1;
}
Int32 LuaBattle::luaGetBattlerFDHurt(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nAP);
	return 1;
}
Int32 LuaBattle::luaGetBattlerGHHurt(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nAP);
	return 1;
}
Int32 LuaBattle::luaSetBattlerHP(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -3);
	Int32 nIndex = lua_tointeger(L, -2);
	Int32 nHP = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	//lua_pushnumber(L, pBattler->m_nHP);
	pBattler->m_nHPCurrent = nHP;
	if(pBattler->m_nHPCurrent > pBattler->m_nHP)
		pBattler->m_nHPCurrent = pBattler->m_nHP;
	if(pBattler->m_nHPCurrent < 0)
		pBattler->m_nHPCurrent = 0;
	return 0;
}
Int32 LuaBattle::luaSetBattlerHPChange(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -3);
	Int32 nIndex = lua_tointeger(L, -2);
	Int32 nHPChange = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	pBattler->m_nHPChange = nHPChange;
	LOGGER_INFO("luaSetBattlerHPChange,nHPChange:%d",nHPChange);
	return 0;
}
Int32 LuaBattle::luaGetBattlerHP(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nHPCurrent);
	return 1;
}
Int32 LuaBattle::luaGetBattlerHPMax(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nHP);
	return 1;
}
Int32 LuaBattle::luaGetBattlerAP(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nAP);
	return 1;
}
Int32 LuaBattle::luaGetBattlerDP(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nDP);
	return 1;
}

Int32 LuaBattle::luaGetBattlerPropBJ(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nPropBJ);
	return 1;
}

Int32 LuaBattle::luaGetBattlerPropSB(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nPropSB);
	return 1;
}

Int32 LuaBattle::luaGetBattlerPropFJ(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nPropFJ);
	return 1;
}

Int32 LuaBattle::luaGetBattlerPropLJ(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nPropLJ);
	return 1;
}

Int32 LuaBattle::luaSendChatMessageBattler(lua_State *L)
{
	const char* szRet = NULL;
	Int32 nTeam = lua_tointeger(L, -3);
	Int32 nIndex = lua_tointeger(L, -2);
	std::string message;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) message = szRet;
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	pBattle->SendChatMessageBattler(nTeam,nIndex,message);
	return 0;
}
/*Int32 LuaBattle::luaGetBattlerState(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	//lua_pushnumber(L, pBattler->m_nState);
	return 1;
}*/


/*Int32 LuaBattle::luaSetBattleEndInfoAllPlayer(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	pBattle->SetBattleEndInfoAllPlayer(info);
	return 0;
}*/

Int32 LuaBattle::luaSendInfoAllPlayer(lua_State *L)
{
	const char* szRet = NULL;
	std::string info;
	szRet = lua_tostring(L, -1);
	if(szRet != NULL) info = szRet;
	if(info.empty())return 0;
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	pBattle->SendInfoAllPlayer(info);
	return 0;
}

Int32 LuaBattle::luaGetBattlerEnhanceHL(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nEnhanceHL);
	return 1;
}
Int32 LuaBattle::luaGetBattlerEnhanceBD(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nEnhanceBD);
	return 1;
}
Int32 LuaBattle::luaGetBattlerEnhanceHS(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nEnhanceHS);
	return 1;
}
/*Int32 LuaBattle::luaGetBattlerEnhanceJZ(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nEnhanceJZ);
	return 1;
}
Int32 LuaBattle::luaGetBattlerEnhanceSD(lua_State *L)
{
	Battle::Battler* pBattler = GetBattler(L);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->m_nEnhanceSD);
	return 1;
}*/

Int32 LuaBattle::luaGetBattlerResistHL(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nResistHL);
	return 1;
}
Int32 LuaBattle::luaGetBattlerResistBD(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nResistBD);
	return 1;
}
Int32 LuaBattle::luaGetBattlerResistHS(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nResistHS);
	return 1;
}
Int32 LuaBattle::luaGetBattlerResistJZ(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nResistJZ);
	return 1;
}
Int32 LuaBattle::luaGetBattlerResistSD(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pBattler->m_nResistSD);
	return 1;
}
Int32 LuaBattle::luaClearBattlerNegativeState(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	pBattler->ClearNegativeState();
	return 0;
}
/*Int32 LuaBattle::luaGetTargetState(lua_State *L)
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		lua_pushnumber(L, Battle::Battler::BATTLER_STATE_NORMAL);
		return 1;
	}
	//lua_pushnumber(L, pBattler->m_nState);
	return 1;
}*/

Int32  LuaBattle::luaIsBattlerStateNormal(lua_State *L)//{return m_arState[BATTLER_STATE_NORMAL];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateNormal()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateDead(lua_State *L)//{return m_nHP <= 0 || m_arState[BATTLER_STATE_DEAD];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateDead()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateTP(lua_State *L)//{return m_arState[BATTLER_STATE_TP];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateTP()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateHL(lua_State *L)//{return m_arState[BATTLER_STATE_HL];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateHL()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateBD(lua_State *L)//{return m_arState[BATTLER_STATE_BD];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateBD()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateCM(lua_State *L)//{return m_arState[BATTLER_STATE_HS];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateHS()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateSL(lua_State *L)//{return m_arState[BATTLER_STATE_SL];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateSL()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateQS(lua_State *L)//{return m_arState[BATTLER_STATE_QS];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateQS()?1:0);
	return 1;
}
Int32  LuaBattle::luaIsBattlerStateRM(lua_State *L)//{return m_arState[BATTLER_STATE_CF];}
{
	Int32 nTeam = lua_tointeger(L, -2);
	Int32 nIndex = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)return 0;
	Battle::Battler* pBattler = pBattle->GetBattler(nTeam,nIndex);
	if (pBattler == NULL)
	{
		return 0;
	}
	lua_pushnumber(L, pBattler->IsStateCF()?1:0);
	return 1;
}

Int32 LuaBattle::luaSetLearning(lua_State *L)
{
	Int32 nLearning = lua_tointeger(L, -1);
	Battle* pBattle = GetBattle(L);
	if (pBattle == NULL)
	{
		return 0;
	}
	pBattle->SetLearning(nLearning==1);
	return 0;
}
Int32 LuaBattle::luaAddPlayerStamina(lua_State *L)
{
	Int32 nCharId = lua_tointeger(L, -2);
	Int32 nValue = lua_tointeger(L, -1);
	Player* pPlayer = singleton<PlayerManager>::instance().GetByCharId(nCharId);
	if (pPlayer == NULL)
	{
		lua_pushnumber(L, 0);
		return 1;
	}
	lua_pushnumber(L, pPlayer->addStamina(nValue)?1:0);
	return 1;
}